package com.example.rabbitmqdemo.listener;

import com.example.rabbitmqdemo.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-04-04
 * Time: 16:37
 */
//@Component
public class DLListener {
    @RabbitListener(queues = Constants.NORMAL_QUEUE)
    public void handleMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.printf("接收到消息: %s ,deliveryTag: %d \n",new String(message.getBody(),"UTF-8"),deliveryTag);

            System.out.println("业务逻辑处理");
            int num = 3 / 0;
            System.out.println("业务逻辑处理完成");
            //肯定确认
            channel.basicAck(deliveryTag,false);
        }catch (Exception e) {
            //否定确认
            channel.basicNack(deliveryTag,false,false);
            //requeue为false，则为死信
        }
    }

    @RabbitListener(queues = Constants.DL_QUEUE)
    public void handleMessage2(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("接收到消息: %s ,deliveryTag: %d \n", new String(message.getBody(), "UTF-8"), deliveryTag);
    }

}
